const { searchRecord, searchItems, getItems } = require("../../utils/feishuAPI");

Page({
  // 页面变量
  data: {
    classGoal: '',
    organGoal: '',
    progress: 0,       // 进度条默认值
    dropdownVisible: false,  // 控制下拉菜单显示
    selectedText: '关联的任务点',  // 默认显示文本
    options: ['选项1', '选项2', '选项3', '选项4']  // 下拉选项
  },
  
  onLoad: function () {
  },
  onShow: function () {
    console.log("current page: post");

    const that = this
    tt.getStorage({
      key: 'tasks',
      success(res) {
        console.log('tasks')
        console.log(res)
        t = []
        index = 0
        for (let i = 0; i < res.data.length; i++) {
          for (let j = 0; j < res.data[i].tasks.length; j++) {
            t[index++] = res.data[i].tasks[j].title
          }
        }
        console.log("titles:", t)
        that.data.options = t
        that.setData({
          options: t
        })
      }
    })

    // 页面显示时更新标题
    const app = getApp();
    app.updateGoal();
    console.log(app.globalData.classGoal)
    console.log(app.globalData.organGoal)
    this.setData({
      classGoal: app.globalData.classGoal,
      organGoal: app.globalData.organGoal,
    })

    this.setData({
      progress: app.globalData.progress
    })
  },

  // 提交
  onSubmit() {
    tt.showToast({
      "title": "提交",
      "duration": 1000,
      "icon": "success",
      success(res) {
        console.log(JSON.stringify(res));
      },
      fail(res) {
        console.log(`showToast fail: ${JSON.stringify(res)}`);
      }
    })
  },
  // 切换下拉菜单显示与隐藏
  toggleDropdown() {
    this.setData({
      dropdownVisible: !this.data.dropdownVisible
    });
  },

  // 选择选项并更新显示文本
  selectOption(e) {
    const selectedValue = e.currentTarget.dataset.value;
    this.setData({
      selectedText: selectedValue,  // 更新显示文本
      dropdownVisible: false        // 收起下拉菜单
    });
  },
  
  onAddAttachment() {
    const that = this
    tt.chooseMedia({
      mediaType: [
          "image",
          "video"
      ],
      sourceType: [
          "album"
      ],
      count: 5,
      sizeType: [
          "compressed",
          "original"
      ],
      maxDuration: 60,
      cameraDevice: "back",
      isSaveToAlbum: '0',
      success(res) {
        console.log(`chooseMedia success: ${JSON.stringify(res)}`);
        let dataArray = res.tempFiles
        console.log(dataArray);
      
        that.setData({ 
          dataArray: that.sortAttachments(dataArray)
        })
      },
      fail(res) {
        console.log(`chooseMedia fail: ${JSON.stringify(res)}`);
      }
    });
  },
  previewImage: function(e) {
    var current = e.target.dataset.src
    const { dataArray } = this.data
    console.log(`dataArray: ${JSON.stringify(dataArray)}`);
    // let urls = dataArray.map((item, index, array) => {
    //     if (item.type == 'image') {
    //         return item.tempFilePath
    //     }
    // })
    let urls = dataArray.filter(item => item.type == 'image' ).map(item => item.tempFilePath)
    console.log(`urls: ${JSON.stringify(urls)}`);
    tt.previewImage({
        current: current,
        urls: urls,
        success: res => {
            console.log(`previewImage success: ${JSON.stringify(res)}`)
            console.log(`this.data.tags: ${JSON.stringify(this.data.tags)}`)
        },
        fail: res => {
            console.log(`previewImage fail: ${JSON.stringify(res)}`)
        }
    })
  },
  sortAttachments(dataArray) {
    // 排序规则：图片在前，视频在后
    return dataArray.sort((a, b) => {
      if (a.type === 'image' && b.type === 'video') return -1;
      if (a.type === 'video' && b.type === 'image') return 1;
      return 0;
    });
  },
});